package wiBase.baseObject;


import wiBase.WiObject;

public class Packet extends WiObject {

    public String id = "";
    /**
     * type of packet.
     */
    public String type = "";
    public String sourceId = "";
    public String sourcePort = "";
    public String destId = "";
    public String destPort = "";
    public String size = "";
    public String payloadSize = "";
    public String payloadContent = "";
    public PacketHeader header = null;

    public Packet() {
	header = new PacketHeader();
    }

    @Override
    public String toString() {
	return "Packet [id=" + id + ", type=" + type + ", sourceId=" + sourceId
		+ ", sourcePort=" + sourcePort + ", destId=" + destId
		+ ", destPort=" + destPort + ", size=" + size
		+ ", payloadSize=" + payloadSize + ", payloadContent="
		+ payloadContent + ", header=" + header + "]";
    }
    // seriable is a event

    public void run(String type, WiObject... seriable) {
	if (getOutputListener() == null) {
	    return;
	}

	Event event = (Event) seriable[0];
	switch (type) {
	    case "send":
	    case "forward":
	    case "receive":
		break;
	    case "drop":
		System.out.println("DROP:" + event.reason);
	    case "move":
	    case "sleep":
	    case "wake":
		break;

	    default:
		break;
	}
	// call back
	Packet _packet_ = this;
	int size = seriable.length;
	WiObject[] seri = new WiObject[size + 1];
	for (int i = 0; i < size; ++i) {
	    seri[i] = seriable[i];
	}
	seri[size] = _packet_;
	getOutputListener().write(type, seri);
    }

    /**
     * inflate variable matching with attrName with value=attrValue.
     */
    public static Packet parseValue(String[] attrName, String[] attrValue) {
	Packet _packet = new Packet();
	for (int i = 0; i < attrName.length; ++i) {
	    switch (attrName[i]) {
	    	case "id":
		    _packet.id = attrValue[i];
		    break;
	    	case "type":
		    _packet.type = attrValue[i];
		    break;
	    	case "source-id":
		    _packet.sourceId=attrValue[i];
		    break;
	    	case "source-port":
		    _packet.sourcePort=attrValue[i];
		    break;
	    	case "dest-id":
		    _packet.destId=attrValue[i]; 
		    break;
	    	case "dest-port":
		    _packet.destPort=attrValue[i];
		    break;
	    	case "size":
		    _packet.size=attrValue[i];
		    break;
	    }
	}
	return _packet;
    }
}
